package cn.lyp.controller;

import cn.lyp.domain.Product;
import cn.lyp.domain.Store;
import cn.lyp.feignclient.IStoreFeignClient;
import cn.lyp.service.ProductService;
import cn.lyp.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private IStoreFeignClient storeFeignClient;

    @PostMapping
    public List<Product> findAll(){
        return productService.findAll();
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody Product product){
        try {
            Store store = product.getStore();
            productService.add(product);
            store.setProduct_id(product.getId());
            storeFeignClient.add(store);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统错误");
        }
    }
}
